package com.example.baiducrabs.bayifu;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.baiducrabs.bayifu.Permissions.VerifyPermissionsUtils;
import com.example.baiducrabs.bayifu.screenrecorder.ScreenRecorderUtils;
import com.example.baiducrabs.bayifu.service.PlayingMusicServices;
import com.example.baiducrabs.bayifu.utils.Common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends Activity implements ScratchImageView.rateListener, View.OnClickListener {
    Handler mHandler = new Handler();
    /**
     * 规定开始音乐、暂停音乐、结束音乐的标志
     */
    public  static final int PLAT_MUSIC=1;
    public  static final int PAUSE_MUSIC=2;
    public  static final int STOP_MUSIC=3;

    private ImageView miv_move;
    private TextView mtv_tips;
    private TextView xYvelocity;
    private TextView maxvelocity;
    private Timer timer = new Timer();
    private int recLen = 5;
    private TextView mtvCountdown;
    private ScratchImageView msample_image;
    private Button startButton;
    private Button stopButton;
    private static final int REQUEST_CODE = 1;
    private Activity mActivity;
    private Button btNext;
    private Button btTheLastOne;
    private Bitmap bmp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        initTimer();
        initPermission();
    }
    


    private void initView() {
        mActivity=this;
        setContentView(R.layout.main);
        msample_image = (ScratchImageView) findViewById(R.id.sample_image);
        mtv_tips = (TextView) findViewById(R.id.tv_tips);
        miv_move = (ImageView) findViewById(R.id.iv_move);
        mtvCountdown = findViewById(R.id.tvCountdown);
        xYvelocity = findViewById(R.id.XYvelocity);
        maxvelocity = findViewById(R.id.Maxvelocity);
        startButton = findViewById(R.id.start);
        stopButton = findViewById(R.id.stop);
        btNext = findViewById(R.id.Next);
        btTheLastOne = findViewById(R.id.TheLastOne);


        GlideUtils.loadImageView(this.getApplicationContext(),R.mipmap.fireworks_0,miv_move);
        msample_image.onSetView(miv_move,mtv_tips,this);
        ViewTreeObserver vto2 = msample_image.getViewTreeObserver();
        vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                msample_image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                setImager(R.drawable.sports,R.drawable.sports22);
            }
        });
    }




    /**
     * Description:设置图片
     * Data: 2018/4/17 19:57
     * @author: lxy
     */
    private void setImager(int drawableA,int drawableB) {
        msample_image.setImageResource(drawableA);
        bmp = BitmapFactory.decodeResource(getResources(),drawableB );
        msample_image.startDraw(bmp);
    }


    private void initListener() {
        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
        btNext.setOnClickListener(this);
        btTheLastOne.setOnClickListener(this);
    }



    @Override
    public void onXYvelocityListener(String xyv) {
        xYvelocity.setText("实时速率--"+xyv);
    }

    @Override
    public void onMaxvelocityListener(String maxv) {
        maxvelocity.setText("最大速率--"+maxv);
    }

    @Override
    public void onActionDownListener() {
        playingmusic(PLAT_MUSIC);
    }

    @Override
    public void onActionUpListener() {
        playingmusic(PAUSE_MUSIC);
    }

        @Override
    public void onClick(View v) {
            int id = v.getId();
            if(id == R.id.start){
                Intent captureIntent = ScreenRecorderUtils.getInstance(mActivity).mMediaProjectionManager.createScreenCaptureIntent();
                startActivityForResult(captureIntent, REQUEST_CODE);
            }else if(id == R.id.stop){
                ScreenRecorderUtils.getInstance(mActivity).stopRecording();
                ScreenRecorderUtils.getInstance(mActivity).SynthesisRecording();
            }else if(id==R.id.Next){
                BlurBitmapUtil.BitmapRecycle(bmp);
                setImager(R.drawable.maoer,R.drawable.maoer22);
            }else if(id==R.id.TheLastOne){
                BlurBitmapUtil.BitmapRecycle(bmp);
                setImager(R.drawable.sports,R.drawable.sports22);
            }
    }


    private void playingmusic(int type) {
        //启动服务，播放音乐
        Intent intent=new Intent(this,PlayingMusicServices.class);
        intent.putExtra("type",type);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        playingmusic(STOP_MUSIC);
        //启动服务，播放音乐
        Intent intent=new Intent(this,PlayingMusicServices.class);
        stopService(intent);
        ScreenRecorderUtils.getInstance(mActivity).onClose();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ScreenRecorderUtils.getInstance(mActivity).startRecording(resultCode,data);
    }

    /**
     * Description:倒计时
     * Data: 2018/4/10 10:56
     * @author: lxy
     */
    private void initTimer() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {      // UI thread
                    @Override
                    public void run() {
                        recLen--;
                        mtvCountdown.setText("倒计时--"+recLen);

                        if(recLen < 1){
                            recLen=3;
                            xYvelocity.setText("实时速率--0");
                            maxvelocity.setText("最大速率--0");
                            msample_image.restart();
//                        timer.cancel();
                        }
                    }
                });
            }
        };
        timer.schedule(task, 1000, 1000);       // timeTask
    }
    /**
     * Description:验证权限
     * Data: 2018/4/10 10:59
     * @author: lxy
     */
    private void initPermission() {
        VerifyPermissionsUtils.verifyPermissions(this);
    }

}

